<?php

/**
 * @author     Thibaut Cromphaut and Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Safti-Kado
 */
class DepartmentController extends Zend_Controller_Action
{

    public function init()
    {
        $this->stylesBase = $this->view->baseUrl() . '/styles/';
    }

    public function indexAction()
    {         
        $view = $this->_getView('ViewDepartment');  
        $department = $this->_getAllDepartments();  
        $view->department = $department;       

    }
    
    public function editAction()
    {   
        $view = $this->_getView('EditDepartment');        
        
        /*$department = $this->_getAllDepartments();  
        $view->departments = $department;*/
        
        $departmentId = (string)$this->getRequest()->getParam('id');         
        $view->id = $departmentId;
        
        $view->departments = $this->_getAllDepartments();               
        $view->shelfs = $this->_getAllShelfsByDepartmentId($departmentId); 
        
        $form = $this->_getEditDepartmentForm();
        
        if ($this->getRequest()->isPost()) 
	{
            $formData  = $this->_request->getPost();
            if ($form->isValid($formData)) 
            {
                $view = $this->_getView('Fout - Toevoegen');                
                // Department toevoegen 
                $values = $form->getValues();
                
                // Set only the interesting values
                $department_values['id'] = $values['department_id'];
                $department_values['name'] = $values['department_name'];
                $department_values['color'] = $values['department_color'];
                
                $department = new Application_Model_Department($department_values);
                $departmentMapper = new Application_Model_DepartmentMapper();
                $departmentMapper->save($department);

                return $this->_helper->redirector('index', 'department');                
            }
        }else{
            $id = (string)$this->getRequest()->getParam('id');
            
            $department = $this->_getViewDepartment($id);
            $view->department = $department;    
            
            foreach($department->Subdepartment as $subdepartment)
            {
                $points[] = $subdepartment->SubdepartmentPoints;                
            }
            if(count($points) < 2)
            {
                $points[1] = '';
            }
            if(count($points) < 1)
            {
                $points[0] = '';
                $points[1] = '';
            }
            
            $form->populate(
                        array(
                        'department_id'            => $department->Id,    
                        'department_name'          => $department->Name,
                        'department_color'         => $department->Color, 
                        'department_points1'       => $points[0],
                        'department_points2'       => $points[1],
                        ));
                        
                $view->form = $form;  
        }   
    }
    
    public function viewAction()
    {
        /*$view = $this->_getView('ViewDepartment');       
        $name = (string)$this->getRequest()->getParam('name');
        $view->department = $this->_getAllDepartments(); */
        //$view->department = $this->_getViewDepartment($name)->toArray();
    }
    /**
     * @param string $title
     * @return Zend_View
     */
    private function _getView($title)
    {
        $view = $this->view;
        $view->title = $title;
        $view->headTitle($view->title, 'prepend');                           // Zend_View_Helper_HeadTitle
        $view->headLink()->appendStylesheet($this->stylesBase . 'main.css'); // Zend_View_Helper_HeadLink

        return $view;
    }
    
    private function _getEditDepartmentForm()
    {   
        return new Application_Form_EditDepartment(array(
            'action' => $this->view->baseUrl() . '/department/edit/',
            'post'   => 'post',
        ));
    }
    
    /**
     * @return Array
     */
    private function _getAllDepartments()
    {
        $departmentMapper = new Application_Model_DepartmentMapper();
        $departmentArray = $departmentMapper->fetchAll();

        return $departmentArray;
    }
    
     /**
     * @return Array
     */
    private function _getDepartmentById($id)
    {
        $departmentMapper = new Application_Model_DepartmentMapper();
        $departmentString= $departmentMapper->fetchByDepartmentId($id);

        return $departmentString;
    }
    
     /**
     * @return Array
     */
    private function _getShelfById($id)
    {
        $shelfMapper = new Application_Model_ShelfMapper();
        $shelf = $shelfMapper->fetchShelfById($id);

        return $shelf;
    }
    
    private function _getAllShelfsByDepartmentId($id)
    {
        $shelfMapper = new Application_Model_ShelfMapper();
        $shelfArray= $shelfMapper->fetchByDepartmentId($id);

        return $shelfArray;
    }
    
    /*private function _getAllSubdepartment()
    {    
        $departmentHasSubdepartmentMapper = new Application_Model_DepartmentHasSubdepartmentMapper();
        $departmentArray = $departmentHasSubdepartmentMapper->fetchAll();

       return $departmentArray;
    }*/
    
    private function _getViewDepartment($id)
    {    
        $departmentMapper = new Application_Model_DepartmentMapper();
        $departmentArray = $departmentMapper->fetchByDepartmentId($id);

        return $departmentArray;
    }
    
    /*private function _getViewSubdepartment($id)
    {    
        $departmentHasSubdepartmentMapper = new Application_Model_DepartmentHasSubdepartmentMapper();
        $departmentArray = $departmentHasSubdepartmentMapper->fetchAllWithDepartmentId($id);

       return $departmentArray;
    }*/



}

